<?php
//默认编码为UTF-8
error_reporting(1);
session_start();
$tabPre = 'pf_';
$conn = mysql_connect('localhost','root','123456') or die('不能连接mysql!');
mysql_select_db('phpgather',$conn) or die('不能选择数据库');
mysql_query('set names utf8',$conn);
header("Content-type: text/html;charset=utf-8"); 

set_time_limit(0);
set_magic_quotes_runtime(0);

//对字符串进行转义
function ds($string, $force = 0) {
	!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
	if(!MAGIC_QUOTES_GPC) {
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = ds($val, $force);
			}
		} else {
			$string = addslashes($string);
		}
	}
	return $string;
}
$_GET  = ds($_GET);
$_POST = ds($_POST);




//把用户输入的字符串转化成php能识别的正则因子
function getPat($string){
	$string=str_replace('\\','\\\\',$string);
	$string=str_replace("(","\(",$string);
	$string=str_replace(")","\)",$string);
	$string=str_replace("[","\[",$string);
	$string=str_replace("]","\]",$string);
	$string=str_replace('"','\"',$string);
	$string=str_replace('.','\.',$string);
	$string=str_replace('?','\?',$string);
	$string=str_replace('$','\$',$string);
	$string=str_replace('^','\^',$string);
	$string=str_replace('/','\/',$string);
	$string=str_replace('+','\+',$string);
	$string=preg_replace("/\{(.*?)\}/eis","replace_preg('\\1')",$string);
	return $string;
}

function replace_preg($string){
	$string=str_replace('\"','"',$string);
	$rule=$string;
	
	if(ereg("^NO",$rule)){
		$detail=explode("NO",$rule);
		return "([^{$detail[1]}]*)";
	}elseif($rule=='*'){
		return "(.*?)";
	}elseif($rule=='**'){
		return "(.*)";
	}else{
		$detail=explode("=",$string);
		$rule=$detail[1];
		if(ereg("^NO",$rule)){
			$detail=explode("NO",$rule);
			return "([^{$detail[1]}]*)";
		}elseif($rule=='*'){
			return "(.*?)";
		}elseif($rule=='**'){
			return "(.*)";
		}
	}
}

function gc($url,$fromChartSet='UTF-8',$contentMustNeed=null){ 
 
	//while($cnt < 3 && ($result=@file_get_contents($url))===FALSE) $cnt++;
	$result = false;
	$curl = curl_init($url); //初始化   
	curl_setopt($curl, CURLOPT_HEADER, FALSE);     
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);  //将结果输出到一个字符串中，而不是直接输出到浏览器     
	curl_setopt($curl, CURLOPT_REFERER, getDomain($url).rand(1,180) ); //最重要的一步，手动指定Referer    
	$result = curl_exec($curl); //执行    
	if (curl_errno($curl)) {   
		curl_close($curl);    
		return NULL; 
	}    
	curl_close($curl);

	if($result==false){
		while($cnt < 3 && ($result=@file_get_contents($url))===FALSE) $cnt++;
	}
	
	if($contentMustNeed != null){
		if(strpos($result,$contentMustNeed)===false){
			@file_get_contents('http://sina.com.cn/');
			sleep(1);
			while($cnt < 3 && ($result=@file_get_contents($url))===FALSE) $cnt++;
		}
	}

	$fromChartSet = strtoupper($fromChartSet);
	if($fromChartSet != 'UTF-8'){
		$result	= mb_convert_encoding($result,'UTF-8',$fromChartSet);
		//$result	= iconv($fromChartSet,'UTF-8',$result);
	}
	return  $result;
}

/*
	功能：获取绝对地址
	$rootUrl 为采集的网页，即基础页
	$url     匹配得到的详细页网址，有
		全绝对路径(http:// 开始的，如 http://www.phpfull.com/news.php)，
		半绝对路径(/ 开始的 ，如/detail.php)，
		和相对路径(.. 或者 直接文件名的 ，如 ../news.php 或者news.php )
	// to do 如果是 .. 开始的还没有处理
*/
function getRealUrl($rootUrl,$url){
	if(!ereg("^http://",$url)){
		if(ereg("^/",$url)){
			$url=preg_replace("/http:\/\/([^\/]+)(.*)/is","http://\\1$url",$rootUrl);
		}elseif(ereg("^./",$url)){
			$url = ltrim($url,'.');
			$url=preg_replace("/http:\/\/([^\/]+)(.*)/is","http://\\1$url",$rootUrl);
		}elseif(ereg("^../",$url)){
			$url = rep2($url,$rootUrl);
			if(!ereg("^http://",$url)){
				$url = str_replace('http:/','http://',$url);
			}
		}else{
			$url=str_replace(basename($rootUrl),"",$rootUrl).$url;
		}
	}
	$pat = '/[\xe0-\xef][\x80-\xbf]{2}/';
	preg_match_all($pat,$url ,$m);
	$length = sizeof($m[0]);
	if($length>0){
		for($i=0;$i<$length;$i++){
			$key = $m[0][$i];
			$url = str_replace($key,urlencode($key),$url);
		}
	}
	return $url;
}
/** 
* 计算绝对路径 
* @param 当前路径 $baseUrl 
* @param 相对路径 $url 
*/
function rep2($url, $baseUrl){ 
	$baseUrl = rtrim($baseUrl,'/');
	$domainUrlArray = parse_url($baseUrl);
	 
	if(isset($domainUrlArray['port']) && $domainUrlArray['port']!= 80 ){
		$domain = $domainUrlArray['scheme'].'://'.$domainUrlArray['host']. ':'.$domainUrlArray['port'] ; 
	}else{
		$domain = $domainUrlArray['scheme'].'://'.$domainUrlArray['host'] ;
	}
	$basePathArray = array();
	if($domainUrlArray['path']!=''){
		$basePathArray = explode('/',$domainUrlArray['path']);
		if($basePathArray[0] == ''){
			array_shift($basePathArray); 
			$maxIndex = sizeof($basePathArray) - 1 ;
			if(strpos($basePathArray[$maxIndex],'.')!==false){
				array_pop($basePathArray);
			}
		} 
	}
	//以下部分为.. 部分
	$urlParentArray = array();
	$urlParentArray = explode('/',$url);
	foreach($urlParentArray as $key=>$value){
		if($value=='..'){
			array_pop($basePathArray);
			array_shift($urlParentArray); 
		}
	}
	$tempUrl = '';
	if( sizeof($basePathArray)>0 ){
		$tempUrl = '/'.implode('/',$basePathArray);	
	} 
	return $domain.$tempUrl.'/'.implode('/',$urlParentArray);
}

function getDomain($url){
	$domain = '/';
	$domain=preg_replace("/http:\/\/([^\/]+)(.*)/is","http://\\1$domain",$url);
	return $domain;
}

//过滤字符串中超链接
function filterStr($str){
	$search = array ("'<a[^>]*?>.*?</a>'si");
	$replace = array ("");
	return preg_replace($search,$replace,$str);
}

//替换指定的字符串
function getReplace($str,$replace){
	$temp = explode("\r\n",$replace);
	$old = array();
	$new = array();
	foreach($temp as $v){
		$t = explode('|',$v);
		$old[] = $t[0];
		$new[] = $t[1];
	}
	return str_replace($old,$new,$str);
}




function p($message,$limit="\t"){
	echo $message.$limit;
	ob_flush();flush();
}

/**
 * 抓取网页内容图片到本地
 * @param string $content 待抓取网页内容 
 * @param string $imgDir  图片保存目录,会自动生成二级目录
 * 
 * 
 * @return 替换图片路径后的内容
 */
function getPic($content, $imgDir){
	
	$imgArray = array();
	//匹配所有的图片链接
	preg_match_all("/(src|SRC)=[\"|'| ]{0,}(http:\/\/(.*)\.(gif|jpg|jpeg|bmp|png))/isU",$content,$imgArray);
	$imgArray = array_unique($imgArray[2]); //图片链接结果
	set_time_limit(0);
	$imgPath = $imgDir."/".strftime("%Y%m%d",time());//保存路径

	$milliSecond = strftime("%H%M%S",time()); //用于生成文件名
	if(!is_dir($imgDir)) @mkdir($imgDir,0777);
	if(!is_dir($imgPath)) @mkdir($imgPath,0777);
	
	$fileArray = array();
	
	foreach($imgArray as $key =>$value)
	{
		$value = trim($value);
		$get_file = @file_get_contents($value);
		$rndFileName = $imgPath."/".$milliSecond.$key.".".substr($value,-3,3);  //文件名
		if($get_file)
		{
			$fp = @fopen($rndFileName,"w");
			@fwrite($fp,$get_file);
			@fclose($fp);
		}
		$content = str_replace($value,$rndFileName,$content);
		if(file_exists($rndFileName)){
			$fileArray[] = $rndFileName;
		}
	}
	return $content;
}
?>